package com.higgins.client.agentapp.components;

import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.higgins.client.agentapp.datamodel.AdvertisedJobDTO;
import com.higgins.client.agentapp.datamodel.AgencyDTO;
import com.higgins.client.agentapp.datamodel.AgentDTO;
import com.higgins.client.agentapp.interfaces.AdvertisedJobListChangeHandler;
import com.higgins.client.agentapp.interfaces.AgencyListListener;
import com.higgins.client.agentapp.interfaces.AgentListChangeHandler;
import com.higgins.client.agentapp.interfaces.AgentListListener;
import com.higgins.client.agentapp.service.AgentAppService;
import com.higgins.client.agentapp.service.AgentAppServiceAsync;

public class AdvertisedJobListBox extends ListBox implements ChangeHandler	{
	private static final Logger log = Logger.getLogger(AdvertisedJobListBox.class.getName());
	private final AgentAppServiceAsync agentAppService = GWT
			.create(AgentAppService.class);
	private Long agencyId = null;
	private Long agentId = null;
	private List<AdvertisedJobDTO> advertisedJobsList = null;
	private AdvertisedJobListChangeHandler changeHandler = null;
	
	public AdvertisedJobListBox()	{
		super();
	}
	public void addAgentListChangeHandler(AdvertisedJobListChangeHandler changeHandler)	{
		this.changeHandler = changeHandler;
		this.addChangeHandler(this);
	}
	public void setAgencyId(Long agencyId)	{
		this.agencyId = agencyId;
		requestDataByAgencyId(agencyId);
	}
	public void setAgentId(Long agentId)	{
		this.agentId = agentId;
		requestDataByAgentId(agentId);
	}
	
	private void requestDataByAgencyId(Long agencyId)	{
		try {
			agentAppService.retrieveAdvertisedJobsByAgencyId(agencyId, new AsyncCallback<List<AdvertisedJobDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("StrategyColumns: " + caught.toString());
					}

					@Override
					public void onSuccess(List<AdvertisedJobDTO> result) {
//						List<String> list = new ArrayList<String>();
//						for (CoveringLetterDTO coveringLetterDTO:result)	{
//							list.add(coveringLetterDTO.getName());
//						}
						applyResultsToListBox(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("StrategyColumns: " + e.toString());
		}			
	}
	private void requestDataByAgentId(Long agentId)	{
		try {
			agentAppService.retrieveAdvertisedJobsByAgentId(agentId, new AsyncCallback<List<AdvertisedJobDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("StrategyColumns: " + caught.toString());
					}

					@Override
					public void onSuccess(List<AdvertisedJobDTO> result) {
//						List<String> list = new ArrayList<String>();
//						for (CoveringLetterDTO coveringLetterDTO:result)	{
//							list.add(coveringLetterDTO.getName());
//						}
						applyResultsToListBox(result);
					}
				});
		} catch (IllegalArgumentException e) {
			log.info("StrategyColumns: " + e.toString());
		}			
	}
	private void applyResultsToListBox(List<AdvertisedJobDTO> advertisedJobDTOs)	{
		advertisedJobsList = advertisedJobDTOs;
		this.clear();
		this.addItem("");
		for (AdvertisedJobDTO advertisedJobDTO:advertisedJobDTOs)	{
			this.addItem(advertisedJobDTO.getJobCode() + " - " + advertisedJobDTO.getDescription());
		}
	}
	@Override
	public void onChange(ChangeEvent event) {
		changeHandler.onAdvertisedJobListChange(event);
	}
	public AdvertisedJobDTO getAdvertisedJobDTO()	{
		Integer selected = this.getSelectedIndex();
		if (selected <= 0 || advertisedJobsList==null)
			return null;
		AdvertisedJobDTO dto = advertisedJobsList.get(selected-1);
		return dto;
	}	
}
